package javalab;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * CS3022 - Lab 1: Java Intro
 * Main program compares performance of linear and binary searches.
 *
 * Program first builds an array of 1000 random large integers, then sorts the array using
 * bubble sort.  After, a value is searched for using both linear and binary search.
 *
 * Program written in Python, to be translated to
 * Java.
 *
 * @author Timothy Riley
 */
public class JavaLab {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//create an empty list
		List<Integer> L1 = new ArrayList<Integer>();
		Random generator = new Random();
		//populate the list with random integers
		for (int i = 0; i < 1000; i++) {
			L1.add(generator.nextInt(10000 - 1) + 1);
		}
		Integer[] unsortedArray = new Integer[L1.size()];
		L1.toArray(unsortedArray);


		//create a sorted array from the unsorted array
		Integer[] sortedArray = SearchSort.bubbleSort(unsortedArray);

		//choose a random (existing) value to search for
		Integer val = unsortedArray[generator.nextInt(999)];
		System.out.println("Search value = " + val);

		//perform searches on the two arrays
		Integer linResult = SearchSort.linearSearch(unsortedArray, val);
		Integer binResult = SearchSort.binarySearch(sortedArray, val);

		System.out.println("Linear search took " + linResult + " comparisons.");
		System.out.println("Binary search took " + binResult + " comparisons.");
	}
}
